$(function() {
    $('.userIcon img').bind('click',clickUserIconEvent);
    connect();
    setLocalPassword();
});

function clickUserIconEvent() {
    let top = $('.userIcon').offset().top;
    let left = $('.userIcon').offset().left;
        layer.ready(function(){
            layer.open({
            title:false,
            closeBtn: 0,
            type: 2,
            shade: [0.1,'#fff'],
            anim: -1,
            isOutAnim :false,
            shadeClose:true,
            offset: [top+46,left-350+56],
            resize:false,
            area: ['350px','100px'],
            content: ['/approval/view/showUserInfo','no']
         });
     });
}

function setLocalPassword() {
    let animal = JSON.parse(sessionStorage.getItem("howAreYou"));
    if(animal === null)
        return;
    if(animal.password == null || animal.password == ''){
        layer.ready(function(){
            layer.open({
            title:false,
            closeBtn: 0,
            type: 2,
            shade: [0.1, '#393D49'],
            resize:false,
            btn: ['Apply'],
            area: ['416px','170px'],
            content: ['/approval/view/showSetLocalPassword','no'],
            yes: function(index, layero){
                let pwd = layero.find("iframe")[0].contentWindow.$("#password").val();
                let pwd2 = layero.find("iframe")[0].contentWindow.$("#password2").val();
                if(pwd ===''){
                    layer.msg("请输入密码")
                    return;
                }
                if(pwd !== pwd2){
                    layer.msg("两次输入密码不一致")
                    return;
                }
                $.ajax({
                    url : "/approval/saveLocalPassword/"+pwd,
                    type : "put",
                    success : function(r) {
                        if (r.code == 0) {
                            sessionStorage.setItem("howAreYou",JSON.stringify(r.hpUser));
                            $("#index001").click();
                            layer.close(index);
                        } else {
                            alert(r.msg)
                        }
                    }
                });
              }
            });
        });
    }else {
        $("#index001").click();
    }
}

function openCreate(url, name, iWidth, iHeight) {
    var url = "/approval/view/showCreate"; //转向网页的地址;
    var name = ""; //网页名称，可为空;
    var iWidth = "1008"; //弹出窗口的宽度;
    var iHeight = "670"; //弹出窗口的高度;
    //window.screen.height获得屏幕的高，window.screen.width获得屏幕的宽
    var iTop = (window.screen.height - 30 - iHeight) / 2; //获得窗口的垂直位置;
    var iLeft = (window.screen.width - 10 - iWidth) / 2; //获得窗口的水平位置;
    window.open(url, name, 'height=' + iHeight + ',innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
}

function connect() {
    var sock = new SockJS("/endpointChat");
    var stompClient = Stomp.over(sock);
    stompClient.connect('guest', 'guest', function(frame) {

        /** 订阅了/user/queue/notifications 发送的消息,这里雨在控制器的 convertAndSendToUser 定义的地址保持一致,
         *  这里多用了一个/user,并且这个user 是必须的,使用user 才会发送消息到指定的用户。
         *  */
        stompClient.subscribe("/user/queue/notifications",handleNotification);
        stompClient.subscribe("/user/queue/mailSendingFailed",handleMailSendingFailed);
    });
}

function handleNotification(message) {
    window.frames["iframe1"].window.refreshTable();
    toastr.options = {
        "closeButton" : true,
        "debug" : false,
        "positionClass" : "toast-bottom-right",
        "showDuration" : "400",
        "hideDuration" : "1000",
        "timeOut" : "7000",
        "extendedTimeOut" : "1000",
        "showEasing" : "swing",
        "hideEasing" : "linear",
        "showMethod" : "show",
        "hideMethod" : "fadeOut",
         onclick: function () {
            alert("You clicked Me!");
        }
    }
    toastr.info(message.body);
}

function handleMailSendingFailed(message) {
    toastr.options = {
        "closeButton" : true,
        "debug" : false,
        "positionClass" : "toast-bottom-right",
        "showDuration" : "400",
        "hideDuration" : "1000",
        "timeOut" : "7000",
        "extendedTimeOut" : "1000",
        "showEasing" : "swing",
        "hideEasing" : "linear",
        "showMethod" : "show",
        "hideMethod" : "fadeOut",
         onclick: function () {
            alert("You clicked Me!");
        }
    }
    toastr.error(message.body);
}

var wrapper = new Vue({
    el : '#wrapper',
    data : {
        total : 0
    }
})